<<<<<<< HEAD
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class System extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
	}
	
	/**
	 * 起始时间计息还款
	 * @param $id
	 */
	function autorepay(){
		$this->load->model('Autorepay_model');
  		$list = $this->Autorepay_model->get_auto_repay_all();
		if(count($list) > 0){
			$this->load->model('Borrow_model');
			foreach($list as $obj){
				$data['repayment_id'] 	= $obj['id'];
				$data['user_id'] 		= $obj['user_id'];
				$data['ip'] 			= '127.0.0.1';
				$ret = $this->Borrow_model->repay($data);
				echo $obj['id'].'-'.$obj['user_id'].'='.$ret;
			}
		}
		echo "repay over";
	}
	
	
	/**
	 * 一点投还款
	 * @param $id
	 */
	function repay(){
		$this->load->model('System_model');
		$list = $this->System_model->borrow_repay();
		if(count($list) > 0){
			$this->load->model('Borrow_model');
			foreach($list as $obj){
				$data['repayment_id'] 	= $obj['id'];
				$data['user_id'] 		= $obj['user_id'];
				$data['ip'] 			= '127.0.0.1';
				$ret = $this->Borrow_model->repay($data);
				echo $obj['id'].'-'.$obj['user_id'].'='.$ret;
			}
		}
		echo "repay over";
	}
	
	/**
	 *即时起息还款
	 * @param $id
	 */
	function lzrepay(){
		$this->load->model('System_model');
		$list = $this->System_model->lzborrow_repay();
		if(count($list) > 0){
			$this->load->model('Borrow_model');
			foreach($list as $obj){
				$data['collection_id'] 	= $obj['id'];
				$data['user_id'] 		= $obj['user_id'];
				$data['ip'] 			= '127.0.0.1';
				$ret = $this->Borrow_model->lz_repay($data);
			}
		}
		echo "lzrepay over";
	}
	
	/**
	 * 
	 * 满标审核通过
	 */
	function reverify(){
		$this->load->model('System_model');
		$list = $this->System_model->borrow_reverify();
		if(count($list) > 0){
			$this->load->model('Borrow_model');
			foreach($list as $obj){
				$data['id']					= $obj['id'];
		  		$data['ip']					= '127.0.0.1';
		  		$data["fstatus"]			= 3;
		  		$data["repayment_remark"]	= '满标审核通过';
		  		$data["repayment_user"]		= 1;
				$ret = $this->Borrow_model->zhs_borrow_review_true($data);
			}
		}
		echo "reverify over";
	}
	
	
	// TODO ****** 20141229 by liub 宝付请求交易结果确认
	//  1-投标 2-满标 3-流标 4-还款 5-充值 6-提现 
	function auto_bf_verify(){
	
		
	
	}
	
	//投标结果查询
	private function tender_query(){
		$_sql = " select * from rsh_borrow_tender where bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		foreach ($result as $value) {
			$this->tender_query_by_id($value['id']);
		}
	}
	
	/** 投标结果查询
	 * @param 	$id 			投标表ID
	 * 			$start_time		查询开始时间 
	 * 			$end_time		查询结束时间 
	 * @return string			返回结果
	 * */
	private function tender_query_by_id($id){
		$_sql = " select * from rsh_borrow_tender where id = {$id} and bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		if( 1 == count($result) ){
			$bf_result =$this->bf_query(1, $result['id'], null, null);
			if( "CSD000" == $bf_result['code']){
				$_update = " update rsh_borrow_tender set bf_trade_status = 3  where id = {$id} ";
				$this->sys->query($_update);
			}else{
			
			}
		}
	}
	
	
	// 满标结果查询
	private function  fullborw_query(){
		$_sql = " select * from rsh_borrow where bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		foreach ($result as $value) {
			$this->fullborw_query_by_id($value['id']);
		}
	}
	
	/**
	 * 满标结果查询
	 *  @param 	$orderid 		满标订单ID
	 * 			$start_time		查询开始时间 
	 * 			$end_time		查询结束时间 
	 * @return string			返回结果
	 * */
	private function  fullborw_query_by_id($id){
		$_sql = " select * from rsh_borrow  where id = {$id} and bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		if( 1 == count($result) ){
			$bf_result =$this->bf_query(2, $result['id'], null, null);
			if( "CSD000" == $bf_result['code']){
				$_update = " update rsh_borrow set bf_trade_status = 3  where id = {$id} ";//满标成功
				$this->sys->query($_update);
			}else{
			
			}
		}
	
	}
	
	//还款结果查询
	private function  repay_query(){
	
		$_sql = " select * from rsh_borrow_repayment where bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		foreach ($result as $value) {
			$this->repay_query_by_id($value['id']);
		}
	}
	
	/**
	 * 还款结果查询
	 * @param 	$orderid 		还款订单ID
	 * 			$start_time		查询开始时间 
	 * 			$end_time		查询结束时间 
	 * @return string			返回结果
	 * */
	private function  repay_query_by_id($id){
		$_sql = " select * from rsh_borrow_repayment where id = {$id} and bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		if( 1 == count($result) ){
			$bf_result =$this->bf_query(4, $result['id'], null, null);
			if( "CSD000" == $bf_result['code']){
				$_update = " update rsh_borrow_repayment set bf_trade_status = 3  where id = {$id} ";//还款成功
				$this->sys->query($_update);
			}else{
			
			}
		}
	
	}
	
	
	//充值结果查询
	private function  recharge_query(){
		$_sql = " select * from rsh_account_recharge where bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		foreach ($result as $value) {
			$this->recharge_query_by_id($value['id']);
		}
	}
	
	/**
	 *充值结果查询 
	* @param 	$orderid 		充值订单ID
	 * 			$start_time		查询开始时间 
	 * 			$end_time		查询结束时间 
	 * @return string			返回结果
	 * */
	private function  recharge_query_by_id($id){
		$_sql = " select * from rsh_account_recharge where id = {$id} and bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		if( 1 == count($result) ){
			$bf_result =$this->bf_query(5, $result['id'], null, null);
			if( "CSD000" == $bf_result['code']){
				$_update = " update rsh_account_recharge set bf_trade_status = 3  where id = {$id} ";//还款成功
				$this->sys->query($_update);
			}else{
			
			}
		}
	
	}
	
	//提现结果查询
	private function  cash_query(){
		$_sql = " select * from rsh_account_cash where bf_trade_status == 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		foreach ($result as $value) {
			$this->cash_query_by_id($value['id']);
		}
	}
	
	/**
	 *提现结果查询
	 * @param 	$orderid 		提现订单ID
	 * 			$start_time		查询开始时间 
	 * 			$end_time		查询结束时间 
	 * @return string			返回结果 
	 * */
	private function  cash_query_by_id($id){
		$_sql = " select * from rsh_account_cash where id = {$id} and bf_trade_status == 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		if( 1 == count($result) ){
			$bf_result =$this->bf_query(6, $result['id'], null, null);
			if( "CSD000" == $bf_result['code']){
				$_update = " update rsh_account_cash set bf_trade_status = 3  where id = {$id} ";//还款成功
				$this->sys->query($_update);
			}else{
			
			}
		}
	}

	
	/**
	 * 宝付查询
	 * 
	 * */
	
	private function bf_query($type,$order_id,$start_time,$end_time){
		$config['order_id'] 	= $order_id;
		$config['type'] 		= $type;
		$config['start_time'] 	= $start_time;
		$config['end_time']   	= $end_time;// 13位
		$this->load->library('baofooSdk/dto/request/P2pQueryDto',$config,'p2pQueryDto');
		$this->load->library('baofooSdk/BaofooRequestService',0,'br');
		$result = $this->br->api_P2pQuery($this->p2pQueryDto);
		return $result;
		
	}
	
	
	
	/*function testlzrepay(){
		$this->load->model('Borrow_model');
		$data['collection_id'] 	= 118186;
		$data['user_id'] 		= 329;
		$data['ip'] 			= '127.0.0.1';
		$ret = $this->Borrow_model->lz_repay($data);
		var_dump($ret);
	}*/
	
	
	
	/**
	function slrepay(){
		$this->load->model('System_model');
		$list = $this->System_model->slborrow_repay();
		if(count($list) > 0){
			$this->load->model('Borrow_model');
			foreach($list as $obj){
				$data['collection_id'] 	= $obj['id'];
				$data['user_id'] 		= $obj['user_id'];
				$data['ip'] 			= '127.0.0.1';
				$ret = $this->Borrow_model->sl_repay($data);
				echo $obj['id'].'-'.$obj['user_id'].'='.$ret;
			}
		}
		echo "lzrepay over";
	}
	
	function yjbrepay(){
		$this->load->model('Yjb_model');
		$this->Yjb_model->repay();
		
//		//回款后自动投30天的
//		$this->load->model('Mydb_model','mydb');
//		$sql = "select id from rsh_yjb_borrow where fstatus in(2,3,4) order by id desc limit 1";
//		$yjb = $this->mydb->query_sql($sql,'row');
//		$this->Yjb_model->yjb_auto_tender($yjb['id']);
	}

	function liubiao(){
		$this->load->model('System_model');
		$list = $this->System_model->liubiao();
		if(count($list) > 0){
			$this->load->model('Borrow_model');
			foreach($list as $obj){
				$data['id'] 		= $obj['id'];
				$data['user_id'] 	= $obj['user_id'];
				$data['ip'] 		= $this->js->get_ip();
				echo $this->Borrow_model->Cancel($data);
			}
		}
		echo "liubiao over";
	}*/
=======
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class System extends CI_Controller {
    
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
	}
	
	/**
	 * 起始时间计息还款
	 * @param $id
	 */
	function autorepay(){
		$this->load->model('Autorepay_model');
  		$list = $this->Autorepay_model->get_auto_repay_all();
		if(count($list) > 0){
			$this->load->model('Borrow_model');
			foreach($list as $obj){
				$data['repayment_id'] 	= $obj['id'];
				$data['user_id'] 		= $obj['user_id'];
				$data['ip'] 			= '127.0.0.1';
				$ret = $this->Borrow_model->repay($data);
				echo $obj['id'].'-'.$obj['user_id'].'='.$ret;
			}
		}
		echo "repay over";
	}
	
	
	/**
	 * 一点投还款
	 * @param $id
	 */
	function repay(){
		$this->load->model('System_model');
		$list = $this->System_model->borrow_repay();
		if(count($list) > 0){
			$this->load->model('Borrow_model');
			foreach($list as $obj){
				$data['repayment_id'] 	= $obj['id'];
				$data['user_id'] 		= $obj['user_id'];
				$data['ip'] 			= '127.0.0.1';
				$ret = $this->Borrow_model->repay($data);
				echo $obj['id'].'-'.$obj['user_id'].'='.$ret;
			}
		}
		echo "repay over";
	}
	
	/**
	 *即时起息还款
	 * @param $id
	 */
	function lzrepay(){
		$this->load->model('System_model');
		$list = $this->System_model->lzborrow_repay();
		if(count($list) > 0){
			$this->load->model('Borrow_model');
			foreach($list as $obj){
				$data['collection_id'] 	= $obj['id'];
				$data['user_id'] 		= $obj['user_id'];
				$data['ip'] 			= '127.0.0.1';
				$ret = $this->Borrow_model->lz_repay($data);
			}
		}
		echo "lzrepay over";
	}
	
	/**
	 * 
	 * 满标审核通过
	 */
	function reverify(){
		$this->load->model('System_model');
		$list = $this->System_model->borrow_reverify();
		if(count($list) > 0){
			$this->load->model('Borrow_model');
			foreach($list as $obj){
				$data['id']					= $obj['id'];
		  		$data['ip']					= '127.0.0.1';
		  		$data["fstatus"]			= 3;
		  		$data["repayment_remark"]	= '满标审核通过';
		  		$data["repayment_user"]		= 1;
				$ret = $this->Borrow_model->zhs_borrow_review_true($data);
			}
		}
		echo "reverify over";
	}
	
	
	// TODO ****** 20141229 by liub 宝付请求交易结果确认
	//  1-投标 2-满标 3-流标 4-还款 5-充值 6-提现 
	function auto_bf_verify(){
	
		
	
	}
	
	//投标结果查询
	private function tender_query(){
		$_sql = " select * from rsh_borrow_tender where bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		foreach ($result as $value) {
			$this->tender_query_by_id($value['id']);
		}
	}
	
	/** 投标结果查询
	 * @param 	$id 			投标表ID
	 * 			$start_time		查询开始时间 
	 * 			$end_time		查询结束时间 
	 * @return string			返回结果
	 * */
	private function tender_query_by_id($id){
		$_sql = " select * from rsh_borrow_tender where id = {$id} and bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		if( 1 == count($result) ){
			$bf_result =$this->bf_query(1, $result['id'], null, null);
			if( "CSD000" == $bf_result['code']){
				$_update = " update rsh_borrow_tender set bf_trade_status = 3  where id = {$id} ";
				$this->sys->query($_update);
			}else{
			
			}
		}
	}
	
	
	// 满标结果查询
	private function  fullborw_query(){
		$_sql = " select * from rsh_borrow where bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		foreach ($result as $value) {
			$this->fullborw_query_by_id($value['id']);
		}
	}
	
	/**
	 * 满标结果查询
	 *  @param 	$orderid 		满标订单ID
	 * 			$start_time		查询开始时间 
	 * 			$end_time		查询结束时间 
	 * @return string			返回结果
	 * */
	private function  fullborw_query_by_id($id){
		$_sql = " select * from rsh_borrow  where id = {$id} and bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		if( 1 == count($result) ){
			$bf_result =$this->bf_query(2, $result['id'], null, null);
			if( "CSD000" == $bf_result['code']){
				$_update = " update rsh_borrow set bf_trade_status = 3  where id = {$id} ";//满标成功
				$this->sys->query($_update);
			}else{
			
			}
		}
	
	}
	
	//还款结果查询
	private function  repay_query(){
	
		$_sql = " select * from rsh_borrow_repayment where bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		foreach ($result as $value) {
			$this->repay_query_by_id($value['id']);
		}
	}
	
	/**
	 * 还款结果查询
	 * @param 	$orderid 		还款订单ID
	 * 			$start_time		查询开始时间 
	 * 			$end_time		查询结束时间 
	 * @return string			返回结果
	 * */
	private function  repay_query_by_id($id){
		$_sql = " select * from rsh_borrow_repayment where id = {$id} and bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		if( 1 == count($result) ){
			$bf_result =$this->bf_query(4, $result['id'], null, null);
			if( "CSD000" == $bf_result['code']){
				$_update = " update rsh_borrow_repayment set bf_trade_status = 3  where id = {$id} ";//还款成功
				$this->sys->query($_update);
			}else{
			
			}
		}
	
	}
	
	
	//充值结果查询
	private function  recharge_query(){
		$_sql = " select * from rsh_account_recharge where bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		foreach ($result as $value) {
			$this->recharge_query_by_id($value['id']);
		}
	}
	
	/**
	 *充值结果查询 
	* @param 	$orderid 		充值订单ID
	 * 			$start_time		查询开始时间 
	 * 			$end_time		查询结束时间 
	 * @return string			返回结果
	 * */
	private function  recharge_query_by_id($id){
		$_sql = " select * from rsh_account_recharge where id = {$id} and bf_trade_status = 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		if( 1 == count($result) ){
			$bf_result =$this->bf_query(5, $result['id'], null, null);
			if( "CSD000" == $bf_result['code']){
				$_update = " update rsh_account_recharge set bf_trade_status = 3  where id = {$id} ";//还款成功
				$this->sys->query($_update);
			}else{
			
			}
		}
	
	}
	
	//提现结果查询
	private function  cash_query(){
		$_sql = " select * from rsh_account_cash where bf_trade_status == 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		foreach ($result as $value) {
			$this->cash_query_by_id($value['id']);
		}
	}
	
	/**
	 *提现结果查询
	 * @param 	$orderid 		提现订单ID
	 * 			$start_time		查询开始时间 
	 * 			$end_time		查询结束时间 
	 * @return string			返回结果 
	 * */
	private function  cash_query_by_id($id){
		$_sql = " select * from rsh_account_cash where id = {$id} and bf_trade_status == 1";
		$this->load->model("System_model","sys");
		$result = $this->sys->query($_sql);
		if( 1 == count($result) ){
			$bf_result =$this->bf_query(6, $result['id'], null, null);
			if( "CSD000" == $bf_result['code']){
				$_update = " update rsh_account_cash set bf_trade_status = 3  where id = {$id} ";//还款成功
				$this->sys->query($_update);
			}else{
			
			}
		}
	}

	
	/**
	 * 宝付查询
	 * 
	 * */
	
	private function bf_query($type,$order_id,$start_time,$end_time){
		$config['order_id'] 	= $order_id;
		$config['type'] 		= $type;
		$config['start_time'] 	= $start_time;
		$config['end_time']   	= $end_time;// 13位
		$this->load->library('baofooSdk/dto/request/P2pQueryDto',$config,'p2pQueryDto');
		$this->load->library('baofooSdk/BaofooRequestService',0,'br');
		$result = $this->br->api_P2pQuery($this->p2pQueryDto);
		return $result;
		
	}
	
	
	
	/*function testlzrepay(){
		$this->load->model('Borrow_model');
		$data['collection_id'] 	= 118186;
		$data['user_id'] 		= 329;
		$data['ip'] 			= '127.0.0.1';
		$ret = $this->Borrow_model->lz_repay($data);
		var_dump($ret);
	}*/
	
	
	
	/**
	function slrepay(){
		$this->load->model('System_model');
		$list = $this->System_model->slborrow_repay();
		if(count($list) > 0){
			$this->load->model('Borrow_model');
			foreach($list as $obj){
				$data['collection_id'] 	= $obj['id'];
				$data['user_id'] 		= $obj['user_id'];
				$data['ip'] 			= '127.0.0.1';
				$ret = $this->Borrow_model->sl_repay($data);
				echo $obj['id'].'-'.$obj['user_id'].'='.$ret;
			}
		}
		echo "lzrepay over";
	}
	
	function yjbrepay(){
		$this->load->model('Yjb_model');
		$this->Yjb_model->repay();
		
//		//回款后自动投30天的
//		$this->load->model('Mydb_model','mydb');
//		$sql = "select id from rsh_yjb_borrow where fstatus in(2,3,4) order by id desc limit 1";
//		$yjb = $this->mydb->query_sql($sql,'row');
//		$this->Yjb_model->yjb_auto_tender($yjb['id']);
	}

	function liubiao(){
		$this->load->model('System_model');
		$list = $this->System_model->liubiao();
		if(count($list) > 0){
			$this->load->model('Borrow_model');
			foreach($list as $obj){
				$data['id'] 		= $obj['id'];
				$data['user_id'] 	= $obj['user_id'];
				$data['ip'] 		= $this->js->get_ip();
				echo $this->Borrow_model->Cancel($data);
			}
		}
		echo "liubiao over";
	}*/
>>>>>>> d47623b610d79a4eeb489500830c00d70933f414
}